qinglan.Canvas.Layer = qinglan.Canvas.Class(function(id,zIndex, width, height) {

	this.id = id;

	this.zIndex = zIndex;

	this.FrameX = width || qinglan.Canvas.Config.frameWidth;

	this.FrameY = height || qinglan.Canvas.Config.frameHeight;

	this.offset = new qinglan.Canvas.Vector2(0,0);

	this.isRender = true;

	this.sprites = {};

	this.canvas = document.createElement('canvas');
	this.canvas.width = this.FrameX;
	this.canvas.height = this.FrameY;
	this.ctx = this.canvas.getContext('2d');

}).methods({
	render : function(){
		this.clear();	
		qinglan.Object.each(this.sprites,function(sprite){
			if(sprite.visible){
				sprite.update(this.ctx);
				sprite.__useSelfRender ? sprite.draw(this.ctx) : qinglan.Canvas.CanvasRender.drawSprite(this.ctx,sprite);
			}
		},this);
	},
	addSprite : function(sprite){
		this.sprites[sprite.id] = sprite;
	},
	clear : function(){
		qinglan.Canvas.CanvasRender.clear(this.ctx, this.FrameX, this.FrameY);
	},
	destory : function(){
		qinglan.Object.each(this.sprites,function(sprite){
			sprite.destroy();
		});
	},
	stop : function(){
		this.isRender = false;
	},
	restart : function(){
		this.isRender = true;
	},
	getCanvas : function(){
		return this.canvas;
	},
	getCtx : function(){
		return this.ctx;
	}
});
